package cl.mt.util.quartz;

import java.text.ParseException;
import java.util.Map;

import javax.servlet.ServletException;
 
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
 
import cl.mt.util.quartz.task.SchedulerJob;
import cl.mt.util.quartz.task.SchedulerTask;
 
public class QuartzPlugin implements PlugIn {
 
	@Override
	public void destroy() {
		//null
	}
 
	@Override
	public void init(ActionServlet servlet, ModuleConfig config)
			throws ServletException {
		System.out.println("QuartzPlugin.......");
	   SchedulerTask task = new SchedulerTask();
 
    	   //specify your sceduler task details
    	   JobDetail job = new JobDetail();
    	   job.setName("jobName");
    	   job.setJobClass(SchedulerJob.class);
 
    	   Map dataMap = job.getJobDataMap();
    	   dataMap.put("schedulerTask", task);
 
    	try{
	    	//configure the scheduler time, run it every 5 seconds
	    	CronTrigger trigger = new CronTrigger();
	    	trigger.setName("runMeJobTesting");
	    	//trigger.setCronExpression("0/5 * * * * ?");0 0/5 14 * * ?
	    	//trigger.setCronExpression("0 0/10 * * * ?");//10 min
	    	trigger.setCronExpression("0 0/30 * * * ?");//30 min
	    	
 
	    	//schedule it
    		Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        	scheduler.start();
        	scheduler.scheduleJob(job, trigger);
 
    	}catch(ParseException e){
    		e.printStackTrace();
    	}catch(SchedulerException e){
    		e.printStackTrace();
    	}
     }
}